package easy;

public class Solution_824 {

    public String toGoatLatin(String sentence) {
        boolean[] flags = new boolean[26];
        flags['a' - 'a'] = true;
        flags['e' - 'a'] = true;
        flags['i' - 'a'] = true;
        flags['o' - 'a'] = true;
        flags['u' - 'a'] = true;
        StringBuffer buffer = new StringBuffer();
        sentence += " ";
        char[] chars = sentence.toCharArray();
        for (int i = 0, count = 0, num = 1; i < chars.length; i++) {
            if (chars[i] == ' ') {
                if (count > 0) {
                    if (num > 1) {
                        buffer.append(' ');
                    }
                    char c = chars[i - count];
                    boolean flag = false;
                    if (c >= 'A' && c <= 'Z') {
                        flag = flags[c - 'A'];
                    } else {
                        flag = flags[c - 'a'];
                    }
                    if (flag) {
                        buffer.append(chars, i - count, count);

                    } else {
                        buffer.append(chars, i - count + 1, count - 1);
                        buffer.append(chars[i - count]);
                    }
                    buffer.append("ma");
                    for (int j = 0; j < num; j++) {
                        buffer.append("a");
                    }
                    num++;
                }
                count = 0;
            } else {
                count++;
            }
        }
        return buffer.toString();
    }

    public static void main(String[] args) {
        Solution_824 model = new Solution_824();
        System.out.println(model.toGoatLatin("I speak Goat Latin"));
        System.out.println(model.toGoatLatin("The quick brown fox jumped over the lazy dog"));
    }
}
